//
//  AppDelegate.swift
//  DianXia
//
//  Created by you&me on 2017/12/14.
//  Copyright © 2017年 you&me. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let provider = MoyaProvider<BCModuleApi>()
    let dispose = DisposeBag()
    private let baidukey = "Wbkwwtm5MDY8xiGhTIy7y3hnAXRRPCNT"
    var location = BMKLocationManager()
    var locationTimer : Timer?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        if let islogin = UserManager.shareManager.isLogin , islogin {
            let vc = UIStoryboard.initialViewControllerFromStoryboard(.Home) as! UIViewController
            self.window?.rootViewController = vc
        }

        UIApplication.shared.applicationIconBadgeNumber = 0
        initIQKeyboardManager()
        BMKLocationAuth.sharedInstance().checkPermision(withKey: baidukey, authDelegate: self)
        setBMKLocation()
        requestAuthor()
        addTimer()
        return true
    }

    func setBMKLocation() {
         location.delegate = self
        location.coordinateType = .BMK09LL
        location.distanceFilter = kCLDistanceFilterNone
        location.desiredAccuracy = kCLLocationAccuracyBest
        location.activityType = .automotiveNavigation
        location.pausesLocationUpdatesAutomatically = false
        location.allowsBackgroundLocationUpdates = true //后台权限
        location.startUpdatingLocation()
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    //应用在正在运行(在前台或后台运行)，点击通知后触发appDelegate代理方法:：didReceiveLocalNotification
    func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
        // 获取通知上绑定的信息
        guard let dict = notification.userInfo , dict.count > 0 else {
            return
        }
    }


    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    private func initIQKeyboardManager() {
        IQKeyboardManager.sharedManager().enable = true
        IQKeyboardManager.sharedManager().shouldResignOnTouchOutside = true
    }

}

extension AppDelegate: BMKLocationAuthDelegate, BMKLocationManagerDelegate {
    func bmkLocationManager(_ manager: BMKLocationManager, didUpdate location: BMKLocation?, orError error: Error?) {

        if error != nil{
            print(error!)
        }

        if let bmLocation = location {
            UserManager.shareManager.lat = "\(bmLocation.location.coordinate.latitude)"
            UserManager.shareManager.lng = "\(bmLocation.location.coordinate.longitude)"
        }

    }
}

